package com.example.jsd2205.vx.emos.aop;


import com.example.jsd2205.vx.emos.common.util.R;
import com.example.jsd2205.vx.emos.config.shiro.ThreadLocalToken;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * AOP切面类,拦截所有的Web方法的返回值,再返回的R对象中添加更新后的令牌.
 * @author admin闫
 */
@Aspect
@Component
public class TokenAspect {

    @Autowired
    private ThreadLocalToken threadLocalToken;

    /**
     * 1)拦截所有Web切面方法
     */
    @Pointcut("execution(public * com.example.jsd2205.vx.emos.controller.*.*(..))")
    public void aspect() {

    }

    /**
     * 判断是否刷新生成新令牌
     * 检查ThreadLocal中是否保存令牌
     * 把新令牌绑定到R对象中
     * @param point
     * @return Object
     * @throws Throwable
     */
    @Around("aspect()")
    public Object around(ProceedingJoinPoint point) throws Throwable {
        R r = (R)point.proceed();
        String token = threadLocalToken.getToken();
        if (token != null) {
            r.put("token",token);
            threadLocalToken.clear();
        }
        return r;
    }
}
